<?php 
date_default_timezone_set('America/New_York');
//queries for main page
//Insert character
//see if character exists
$char_str = addslashes($char);
$realm_str = addslashes($realm);
$numSkills = 6; //hardcoded number of skills used in account page

//look for character
$sqlFindChar = mysqli_query($mysqli,"SELECT char_id FROM char_summary WHERE upper(name) like upper('$char_str') and upper(realm) like upper('$realm_str')");
//insert character record
$sqlInsertChar = "INSERT into char_summary (name, realm, race, class, faction, datestamp, userstamp) values ('$char_str', '$realm_str', '$race', '$class', '$faction',(select now()), 1)";

$sqlFindCharRow = mysqli_fetch_row($sqlFindChar);
//get count for results
$sqlNumCharRow = mysqli_num_rows($sqlFindChar);
//get character id
$char_id = $sqlFindCharRow[0];

//Insert history main record
//see if history record exists for today
$sqlSelectHistory = mysqli_query($mysqli,"SELECT char_history_id FROM char_history WHERE char_id='$sqlFindCharRow[0]' AND datestamp = (select curdate())");
//insert history record for today
$sqlLogin = date('Y-m-d',strtotime($lastLogin));
$sqlCreateHistory = "INSERT into char_history (char_id, last_login, points, level, spec1, spec2, datestamp, userstamp) 
values ('$char_id','$sqlLogin','$points','$level','$talent1','$talent2',(select curdate()),1)";
$sql_r_hist = mysqli_fetch_row($sqlSelectHistory);
//count history rows
$sqlHistRow = mysqli_num_rows($sqlSelectHistory); 

//account queries
//define user id
$user_id = $_SESSION['user'];

//find character in account
$sqlSelectChar1 = mysqli_query($mysqli,"SELECT user_char_id FROM user_char WHERE user_id = '$user_id' AND char_id = '$char_id'");
//add character to account
$sqlInsertUserChar = "INSERT into user_char (user_id, char_id, datestamp, userstamp) VALUES ('$user_id','$char_id',(select now()),1)";						 
//get preferences
$sqlSelectPrefs = mysqli_query($mysqli,"SELECT value FROM user_preferences WHERE user_id = '$user_id' and preference like '%hide_inactive%'");
$sqlPrefsRow  = mysqli_fetch_row($sqlSelectPrefs);
$hideInactive = $sqlPrefsRow[0];

$sqlSelectPrefs2 = mysqli_query($mysqli,"SELECT value FROM user_preferences WHERE user_id = '$user_id' and preference like '%warning_date%'");
$sqlPrefsRow2  = mysqli_fetch_row($sqlSelectPrefs2);
$warningPref = $sqlPrefsRow2[0];
	
//find all characters on account
if ($hideInactive==1)
{
$sqlSelectChars = mysqli_query($mysqli,
"SELECT cs.name,cs.realm,cs.race,cs.class,cs.faction,cs.char_id 
FROM char_summary cs INNER JOIN user_char uc 
ON cs.char_id = uc.char_id
LEFT OUTER JOIN char_history ch
ON cs.char_id = ch.char_id
WHERE uc.user_id = '$user_id'
AND cs.active = 1
GROUP BY cs.name, cs.realm, cs.race, cs.class, cs.faction, cs.char_id
ORDER BY ch.level desc");
}
else
{
$sqlSelectChars = mysqli_query($mysqli,
"SELECT cs.name,cs.realm,cs.race,cs.class,cs.faction,cs.char_id 
FROM char_summary cs INNER JOIN user_char uc 
ON cs.char_id = uc.char_id
LEFT OUTER JOIN char_history ch
ON cs.char_id = ch.char_id
WHERE uc.user_id = '$user_id'
GROUP BY cs.name, cs.realm, cs.race, cs.class, cs.faction, cs.char_id
ORDER BY ch.level desc");
}

?>